//
//  GroupToolbarCell.m
//  Guazi
//
//  Created by koolearn on 11-5-26.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "GuaziAppDelegate.h"
#import "GroupToolbarCell.h"
#import "FriendGroup.h"
#import "User.h"
#import "MessagePicker.h"
#import "GroupAddViewController.h"
#import "GroupUserListViewController.h"
#import "FriendsViewController.h"
#import "GroupService.h"


@interface GroupToolbarCell(private)
- (void) buildToolbar;
- (void) setToolbarStatus;
- (void) postTweet;
- (void) postDirectMessage;
- (void) sendMessage;
- (void) editGroup;
- (void) deleteGroup;
- (void) member;
@end


@implementation GroupToolbarCell

@synthesize toolbar;
@synthesize spinner;
@synthesize group;


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self buildToolbar];
        [self.contentView addSubview:toolbar];
        
        self.spinner = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray] autorelease];
        spinner.hidden = YES;
        [spinner stopAnimating];
        [self.contentView addSubview:spinner];
    }
    return self;
}


- (void) updateWithGroup:(FriendGroup*)g
{
    self.group = g;
    if ( group.loading ) {
        [spinner startAnimating];
        spinner.hidden = NO;
    }
    else {
        [spinner stopAnimating];
        spinner.hidden = YES;
    }
    
    [self setToolbarStatus];
}


- (void) buildToolbar
{
    NSArray* items = [NSArray arrayWithObjects:@"动态",@"私信",@"短信",@"成员",@"设置",@"删除", nil];
    UISegmentedControl* sc = [[UISegmentedControl alloc] initWithItems:items];
    sc.segmentedControlStyle = UISegmentedControlStyleBar;
    //sc.tintColor = [UIColor colorWithRed:0.9f green:0.9f blue:0.9f alpha:1.0f];
 
    [sc addTarget:self action:@selector(toolbarSelected) forControlEvents:UIControlEventValueChanged];
    
    self.toolbar = sc;
    [sc release];
}


- (void) setToolbarStatus
{
    BOOL canTwitter = NO;
    BOOL canDM = NO;
    BOOL canMessage = NO;

    for ( User* user in group.friends ) {
        if ( user.phone && [user.phone length]>0 ) {
            canMessage = YES;
        }
        if ( user.userId > 0 ) {
            canTwitter = YES;
            if ( [user.relation compare:kUserRelationBoth] == NSOrderedSame ) {
                canDM = YES;
            }
        }
        
        if ( canMessage && canTwitter && canDM ) break;
    }
    
    BOOL canEdit = YES;
    BOOL canDelete = YES;
    BOOL canMember = YES;
    
    if ( group.groupId == 0 ) {
        canEdit = NO;
        canDelete = NO;
        canMember = NO;
    }
    
    [toolbar setEnabled:canTwitter forSegmentAtIndex:0];
    [toolbar setEnabled:canDM forSegmentAtIndex:1];
    [toolbar setEnabled:canMessage forSegmentAtIndex:2];
    [toolbar setEnabled:canMember forSegmentAtIndex:3];
    [toolbar setEnabled:canEdit forSegmentAtIndex:4];
    [toolbar setEnabled:canDelete forSegmentAtIndex:5];
}


- (void) layoutSubviews
{
    spinner.frame = CGRectMake(5, 5, 30, 30);
    toolbar.frame = CGRectMake(60, 5, 250, 30);
    [super layoutSubviews];
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


- (void) toolbarSelected
{
    int selectedIndex = toolbar.selectedSegmentIndex;
    toolbar.selectedSegmentIndex = -1;
    
    if ( selectedIndex == 0 ) {
        //动态
        [self postTweet];
    }
    else if ( selectedIndex == 1 ) {
        [self postDirectMessage];
    }
    else if ( selectedIndex == 2 ) {
        [self sendMessage];
    }
    else if ( selectedIndex == 3 ) {
        [self member];
    }
    else if ( selectedIndex == 4 ) {
        [self editGroup];
    }
    else if ( selectedIndex == 5 ) {
        [self deleteGroup];
    }
}


//发动态
- (void) postTweet
{
    GuaziAppDelegate *appDelegate = [GuaziAppDelegate getAppDelegate];
    PostViewController* postView = appDelegate.postView;
	[postView postGroup:group];
}


//fa私信
- (void)postDirectMessage
{
    GuaziAppDelegate *appDelegate = [GuaziAppDelegate getAppDelegate];
    PostViewController* postView = appDelegate.postView;
	[postView editDirectMessageGroup:group];
}


//发短信
- (void) sendMessage
{
	NSMutableArray* array = [NSMutableArray array];;
	for ( User* user in group.friends ) {
		if ( user.phone != nil && [user.phone length] > 0 ) {
			[array addObject:user.phone];
		}
	}
	
	if ( [array count] > 0 ) {
        UIViewController* controller = [[GuaziAppDelegate getAppDelegate] currentViewController];
		MessagePicker* picker = [[MessagePicker alloc] initWithController:controller recienpts:array body:@""];
		[picker send];
		[picker release];
	}
}


//编辑组
- (void) editGroup
{
	GroupAddViewController* controller = [[GroupAddViewController alloc] init];
	controller.group = group;

    UINavigationController* nav = [[GuaziAppDelegate getAppDelegate] currentNavigationController];
	[nav pushViewController:controller animated:YES];
	[controller release];
}


//组内成员
- (void) member
{
	GroupUserListViewController* controller = [[GroupUserListViewController alloc] initWithGroup:group];
    UINavigationController* nav = [[GuaziAppDelegate getAppDelegate] currentNavigationController];
	[nav pushViewController:controller animated:YES];
	[controller release];
}


//删除组
- (void) deleteGroup
{
	UIActionSheet* sheet = [[UIActionSheet alloc] initWithTitle:@"删除组后，本组内的联系人将变成未分组状态。是否确认删除本组？" 
													   delegate:self 
											  cancelButtonTitle:@"取消" 
										 destructiveButtonTitle:@"确认" otherButtonTitles:nil];
	[sheet showInView:self.contentView];
	[sheet release];
}


- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
	if ( buttonIndex == 0 ) {
        BOOL result = [GroupService deleteGroup:group.groupId];
        if ( result ) {
            FriendsViewController* controller = (FriendsViewController*) [[GuaziAppDelegate getAppDelegate] currentTabRootViewController];
            [controller refreshDeleteGroup:group];
        }
        
        UINavigationController* nav = [[GuaziAppDelegate getAppDelegate] currentNavigationController];
		[nav popViewControllerAnimated:YES];
	}
}


- (void)dealloc
{
    [group release];
    [super dealloc];
}

@end
